<template>
	<view style="background: #AEE2DA;min-height: 100vh;">
		<cu-custom bgColor="bg-newblue" shop="true" :isBack="true"><block slot="content">安全设置</block></cu-custom>
		<view class="padding">
			<view style="box-shadow:3px 3px 3px 0px #91c6cb;">
				<view class="cu-form-group margin-top">
					<view class="title">实名认证</view>
					<text class='color-safe'>已认证</text>
				</view>
				<view class="cu-form-group"  @click="bindmobile()">
					<view class="title">绑定手机</view>
					<text class='color-safe'>{{mobilebind==0?'未绑定':'已绑定'}}</text>
				</view>
				<view class="cu-form-group"  @click="toPage('login','changepassword')">
					<view class="title">修改密码</view>
					<text class='cuIcon-right text-gray'></text>
				</view>
			</view>
			<view style="box-shadow:3px 3px 3px 0px #91c6cb;">
				<view class="cu-form-group margin-top" @click="bindwx()">
					<view class="title">微信</view>
					<text class='color-safe'>{{wexinbind==0?'未绑定':'已绑定'}}</text>
				</view>
				<view class="cu-form-group" @click="bindali()">
					<view class="title">支付宝</view>
					<text class='color-safe'>{{alibind==0?'未绑定':'已绑定'}}</text>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
  export default {
    data() {
		return {
			Imgbase:getApp().globalData.Imgbase,
			wexinbind:0,
			alibind:0,
			mobilebind:0,
		}
    },
    methods: {
		toPage(folder,name,id=-1){
			if(id==-1){
				uni.navigateTo({
					url: `/pages/${folder}/${name}`
				});
			}else{
				uni.navigateTo({
					url: `/pages/${folder}/${name}?id=${id}`
				});
			}
		},
		loadsafe(){
			let _this = this;
			uni.request({
				url: _this.$globals.apibase+'/api/Applogin/safemessage',
				data: {
					token: uni.getStorageSync('token'),
				},
				method: 'POST',
				header: {
					'content-type': 'application/json'
				},
				success: (res) => {
					if(res.data.code == '401'){
						uni.setStorageSync('token', '');
						_this.$uniApi.trylogin();
					}
					if(res.data.code == '1'){
						if(res.data.data.unique.unionid != null){
							_this.wexinbind = 1;
						}
						if(res.data.data.mobile != null){
							_this.mobilebind = 1;
						}
					}
				}
			});
		},
		bindwx(){
			if(this.wexinbind == 0){
				let _this = this;
				uni.login({
				  provider: 'weixin',
				  success: function (loginRes) {
				    // 获取用户信息
				    uni.getUserInfo({
						provider: 'weixin',
						success: function (infoRes) {
							uni.request({
								url: _this.$globals.apibase+'/api/Applogin/openidbind',
								data: {
									token: uni.getStorageSync('token'),
									openid: loginRes.authResult.openid,
									unionid: loginRes.authResult.unionid,
								},
								method: 'POST',
								header: {
									'content-type': 'application/json'
								},
								success: (res) => {
									if(res.data.code == '1'){
										uni.showToast({
											icon: 'none',
											title: '绑定成功'
										});
										_this.loadsafe()
									}
									if(res.data.code == '0'){
										uni.showToast({
											icon: 'none',
											title: res.data.msg
										});
									}
								}
							}); 
						}
				    });
				  }
				});
			}
		},
		bindmobile(){
			if(this.mobilebind == 0){
				uni.navigateTo({
					url: `/pages/login/bindmobile`
				});
			}
		},
		bindali(){
			let urls='https://authweb.alipay.com/auth?auth_type=PURE_OAUTH_SDK&app_id=2021003101623935&scope=auth_user&state=XXXXXXX';
			urls=encodeURIComponent(urls);//将地址编码成浏览器访问的格式
			// 判断平台
			if (plus.os.name == 'Android') {
			  plus.runtime.openURL(
			    'alipays://platformapi/startapp?appId=20000067&url=' + urls,
			    res => {
				//这里写打开URL地址失败后的处理
			      console.log(res);
			      uni.showModal({
			        content: '本机未检测到对应客户端，是否打开浏览器访问页面？',
			        success: function (res) {
			          if (res.confirm) {
			            //plus.runtime.openURL();
			          }
			        }
			      });
			    },
			    'com.eg.android.AlipayGphone'
			  );
			} else if (plus.os.name == 'iOS') {
			  plus.runtime.openURL(
			    'alipay://platformapi/startapp?appId=20000067&url=' + urls,
			    res => {
			      console.log(res);
			      uni.showModal({
			        content: '本机未检测到对应客户端，是否打开浏览器访问页面？',
			        success: function (res) {
			          if (res.confirm) {
			            //plus.runtime.openURL(url);
			          }
			        }
			      });
			    },
			    'com.eg.android.AlipayGphone'
			  );
			}
		},
    },
    onLoad(options) {
		this.loadsafe()
    },
	onUnload() {
	},
  }
</script>

<style>
	.fl{
		float: left;
	}
	.fr{
		float: right;
	}
	.color-safe{
		color: #fc731e;
	}
</style>
